import { createSlice } from '@reduxjs/toolkit'
import axios from 'axios'

const users = createSlice({
  name: 'user',
  initialState: {
    userInfo: {},
  },
  reducers: {
    setUserInfo(state, action) {
      state.userInfo = action.payload
    },
  },
})

const { setUserInfo } = users.actions
//异步数据
const getUserList = () => {
  return async dispatch => {
    const res = await axios.get('http://localhost:3004/users')
    dispatch(setUserInfo(res.data))
  }
}

export { getUserList }
const reducer = users.reducer
export default reducer
